<template>
  <div class="newAddress">
    <van-nav-bar
      :title="aid ? '编辑收货地址' :'新建收货地址'"
      left-text="返回"
      :fixed="true"
      left-arrow
      @click-left="back"
    ></van-nav-bar>

    <van-address-edit
      :area-list="areaList"
      show-delete
      show-set-default
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      :address-info='addressInfo'
      @save="onSave"
      @delete="onDelete"
    />
  </div>
</template>

<script>
import { areaList } from '@vant/area-data'
export default {
  name: "NewAddress",
  data(){
      return{
          areaList,
          addressInfo:{},
          aid:'',
          id:''
      }
  },
  created(){
      this.aid = this.$route.query.aid
      if(this.aid){
          this.getAddress()
      }
  },
  methods:{
      back(){
          this.$router.go(-1)
      },
      addAddress(content){
          let addressInfo = {...content}
          addressInfo.isDefault = Number(addressInfo.isDefault)

          this.axios({
              method:'post',
              url:'/addAddress',
              data:addressInfo
          }).then(result=>{

              if(result.data.code == 9000){
                  this.$toast.success(result.data.msg);
                  setTimeout(()=>{
                      this.$router.go(-1)
                  },2000)
              }
          })
      },
      getAddress(){
           this.axios({
               method:'get',
               url:'/findAddressByAid',
               params:{
                   aid:this.aid
               }
           }).then(result=>{

               if(result.data.code == 40000){
                   let data = result.data.result[0]
                   data.isDefault = Boolean(data.isDefault)
                   this.addressInfo = data
                   let id = result.data.result[0].id
                   this.id = id
               }
           })
      },
      editAddress(content){
          let addressInfo = {
              ...content,
              aid:this.aid
          }

          addressInfo.isDefault = Number(addressInfo.isDefault)

          this.axios({
              method:'post',
              url:'/editAddress',
              data:addressInfo
          }).then(result=>{

              if(result.data.code == 30000){
                  this.$toast('地址修改成功，3秒后自动返回上一页')
                  setTimeout(()=>{
                      this.$router.go(-1)
                  },3000)
              }
          })
      },
      onSave(content){
          if(this.aid){
              this.editAddress(content)
          }else{
              this.addAddress(content)
          }
      },
      onDelete(){
          this.axios({
              method:'post',
              url:'/deleteAddress',
              data:{
                  aid:this.aid
              }
          }).then(result=>{

              if(result.data.code == 10000){
                  this.$toast('地址已删除')
                  setTimeout(()=>{
                      this.$router.go(-1)
                  },1500)
              }
          })
        let id = localStorage.getItem('id')
          if(this.id == id){
            localStorage.removeItem('id')
          }
      }
  }
};
</script>

<style lang="less" scoped>
.newAddress{
    padding-top: 76px;
  .van-nav-bar--fixed{
    padding-top: 30px;
    background-color: #0022ab;
  }
  /deep/.van-nav-bar__title{
    color: #fff;
  }
  /deep/.van-icon{
    color: #fff;
  }
  /deep/.van-nav-bar__text{
    color: #fff;
  }
}
</style>